package com.d5xx.hunters;

import com.d5xx.hunters.API.Hunter;
import com.d5xx.hunters.API.HunterPlayer;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;

import java.io.File;
import java.io.IOException;

import static com.d5xx.hunters.Files.*;
import static com.d5xx.hunters.Variables.*;

public interface Functions {
    static void load(boolean reload) {
        folders();
        config();
        langFile();
        storageFile();
        Hunter.loadAllLangs();
    }

    static void setLobby(Location loc) {
        File storageFile = new File(dataFolder, "storage.yml");
        YamlConfiguration storage = YamlConfiguration.loadConfiguration(storageFile);
        World world = loc.getWorld();
        if (world == null) {
            return;
        }
        storage.set("lobby.world", world.getName());
        storage.set("lobby.x", loc.getX());
        storage.set("lobby.y", loc.getY());
        storage.set("lobby.z", loc.getZ());
        storage.set("lobby.pitch", loc.getPitch());
        storage.set("lobby.yaw", loc.getYaw());
        try {
            storage.save(storageFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static void cleanAllScoreboard() {
        ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
        if (scoreboardManager == null) {
            return;
        }
        Scoreboard scoreboard = scoreboardManager.getMainScoreboard();
        for(Objective objective : scoreboard.getObjectives()) {
            scoreboard.resetScores(objective.getName());
            objective.unregister();
        }
    }

    static void cleanScoreboard(String name) {
        ScoreboardManager scoreboardManager = Bukkit.getScoreboardManager();
        if (scoreboardManager == null) {
            return;
        }
        Scoreboard scoreboard = scoreboardManager.getMainScoreboard();
        scoreboard.resetScores(name);
        Objective objective = scoreboard.getObjective(name);
        if (objective != null) {
            objective.unregister();
        }
    }

    static void sendWaitingActionaBar(HunterPlayer player) {
        int[] a = {0, 1, 2, 3, 2, 1};
        final int[] i = {0};

        new BukkitRunnable() {
            @Override
            public void run() {
                if (player.getType().equals("lobby-waiting")) {
                    String text = player.getKeyMessage("player-waiting-actionbar");
                    player.getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(text + ".".repeat(a[i[0]])));
                    i[0]++;
                    if (i[0] == a.length) { i[0] = 0; }
                } else {
                    this.cancel();
                }
            }
        }.runTaskTimerAsynchronously(plugin, 0L, 10L);
    }

}
